package com.ymatou.api.marketingseller.testcase;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CancelOrEndPromotionCall;
import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.api.marketingseller.service.GetPromotionProductByIdCall;
import com.ymatou.api.marketingseller.service.SellerPromotionListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.database.model.YmtSellerPromotion;
import com.ymttest.database.sqlwapper.YmtSellerPromotionIMapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

import sun.util.calendar.CalendarUtils;

/**
 * 
 * @author chencan
 *
 */
public class Ts_SellerPromotionList {
	private static SellerPromotionListCall sellerPromotionListCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买手促销活动详情");
	}

	@Before
	public void caseUp() {
		sellerPromotionListCall = new SellerPromotionListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerPromotionListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionList_001(){
		Logger.start(true, "获取买手促销活动详情");
		try{
			int sellerId = 20323743;
			// 创建活动
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->SellerId",sellerId);
			map.put("PromotionInfo->PromotionName","满2件减10-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsSpecifyProduct",false);
			map.put("PromotionInfo->IsLimitProduct",false);
			map.put("PromotionInfo->IsSameProductUse",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			//获取创建活动报文
			JSONObject conflictPromotions = retBea.getJSONObject("Data").getJSONArray("ConflictPromotions").getJSONObject(0);
			String promotionName = conflictPromotions.getString("PromotionName");
			Date promotionBeginTime = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(conflictPromotions.getString("PromotionBeginTime")));
			Date promotionEndTime =  YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(conflictPromotions.getString("PromotionEndTime")));
			String strBegin = YMTDateUtil.getFormatTime(promotionBeginTime, "yyyy-MM-dd");
			String strEnd = YMTDateUtil.getFormatTime(promotionEndTime, "yyyy-MM-dd");
			int promotionType = conflictPromotions.getInt("PromotionType");
			int promotionState = conflictPromotions.getInt("PromotionState");
			int promotionId = conflictPromotions.getInt("PromotionId");
			int seller = conflictPromotions.getInt("SellerId");
			JSONObject jsonObject = new SellerPromotionListCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionName", promotionName,
					"PromotionBeginTime", strBegin,"PromotionEndTime", strEnd,"PromotionType", promotionType,"PromotionState", promotionState));
			
			//获取报文返回结果
			JSONObject promotions = jsonObject.getJSONObject("Data").getJSONArray("Promotions").getJSONObject(0);
			int code =jsonObject.getInt("Code");
			String name = promotions.getString("PromotionName");
			int type =promotions.getInt("PromotionType");
			int id =promotions.getInt("PromotionId");
			int state =promotions.getInt("PromotionState");
			int sellerid = promotions.getInt("SellerId");
			
			//断言
			Logger.verifyEquals(200, code, "响应Code是否等于200");
			Logger.verifyEquals(promotionName, name, "活动名称");
			Logger.verifyEquals(seller, sellerid, "买手ID");
			Logger.verifyEquals(promotionType, type, "活动类型");
			Logger.verifyEquals(promotionId, id, "活动ID");
			Logger.verifyEquals(promotionState, state, "活动状态");
			Logger.verifyNotNull(promotions, "返回结果是否为空");
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionList_002(){
		Logger.start(true, "获取买手促销活动--买手无促销活动");
		try{

			int sellerId = 20239184;
			String promotionName="满2件";
			Date promotionBeginTime= YMTDateUtil.parseYMDDate("2016-10-20");
			Date promotionEndTime= YMTDateUtil.parseYMDDate("2016-11-19");
			String strBegin = YMTDateUtil.getFormatTime(promotionBeginTime, "yyyy-MM-dd");
			String strEnd = YMTDateUtil.getFormatTime(promotionEndTime, "yyyy-MM-dd");
			int promotionType = -1;
			int promotionState = -1;
			
			JSONObject jsonObject = new SellerPromotionListCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionName", promotionName,
					"PromotionBeginTime", strBegin,"PromotionEndTime", strEnd,"PromotionType", promotionType,"PromotionState", promotionState));
			
			// 获取报文返回结果
			JSONObject data = jsonObject.getJSONObject("Data");
			JSONArray promotions = data.getJSONArray("Promotions");
			int code =jsonObject.getInt("Code");
			int size = promotions.length();
			int totalCount = data.getInt("TotalCount");
			
			// 断言
			Logger.verifyEquals(200, code, "响应Code是否等于200");
			Logger.verifyEquals(0, totalCount, "买手无促销活动");
			Logger.verifyEquals(0, size, "活动不存在，结果为空");
			
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionList_003(){
		Logger.start(true, "获取买手促销活动--买手不存在");
		try{

			int sellerId = 10000001;
			String promotionName="满2件";
			Date promotionBeginTime= YMTDateUtil.parseYMDDate("2016-10-20");
			Date promotionEndTime= YMTDateUtil.parseYMDDate("2016-11-19");
			String strBegin = YMTDateUtil.getFormatTime(promotionBeginTime, "yyyy-MM-dd");
			String strEnd = YMTDateUtil.getFormatTime(promotionEndTime, "yyyy-MM-dd");
			int promotionType = -1;
			int promotionState = -1;
			
			JSONObject jsonObject = new SellerPromotionListCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionName", promotionName,
					"PromotionBeginTime", strBegin,"PromotionEndTime", strEnd,"PromotionType", promotionType,"PromotionState", promotionState));
			
			// 获取报文返回结果
			JSONObject data = jsonObject.getJSONObject("Data");
			JSONArray promotions = data.getJSONArray("Promotions");
			int code =jsonObject.getInt("Code");
			int size = promotions.length();
			int totalCount = data.getInt("TotalCount");
			
			// 断言
			Logger.verifyEquals(200, code, "响应Code是否等于200");
			Logger.verifyEquals(0, totalCount, "买手无促销活动");
			Logger.verifyEquals(0, size, "活动不存在，结果为空");
			
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionList_004(){
		Logger.start(true, "获取买手促销活动--时间范围大于30天");
		try{

			int sellerId = 20354944;
			String promotionName="满2件";
			Date promotionBeginTime= YMTDateUtil.parseYMDDate("2016-10-20");
			Date promotionEndTime= YMTDateUtil.parseYMDDate("2016-12-19");
			String strBegin = YMTDateUtil.getFormatTime(promotionBeginTime, "yyyy-MM-dd");
			String strEnd = YMTDateUtil.getFormatTime(promotionEndTime, "yyyy-MM-dd");
			int promotionType = -1;
			int promotionState = -1;
			
			JSONObject jsonObject = new SellerPromotionListCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionName", promotionName,
					"PromotionBeginTime", strBegin,"PromotionEndTime", strEnd,"PromotionType", promotionType,"PromotionState", promotionState));
			
			// 获取报文返回结果
			String  data = jsonObject.getString("Data");
			int code = jsonObject.getInt("Code");
			String msg = jsonObject.getString("Msg");
			
			// 断言
			Logger.verifyEquals(201, code, "响应Code是否等于200");
			Logger.verifyEquals("促销时间跨度范围不能＞30天", msg, "时间范围大于30天");
			Logger.verifyEquals("null", data, "返回结果为null");
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionList_005(){
		Logger.start(true, "获取买手促销活动--不存在的活动类型");
		try{

			int sellerId = 20354944;
			String promotionName="满2件";
			Date promotionBeginTime= YMTDateUtil.parseYMDDate("2016-11-15");
			Date promotionEndTime= YMTDateUtil.parseYMDDate("2016-12-14");
			String strBegin = YMTDateUtil.getFormatTime(promotionBeginTime, "yyyy-MM-dd");
			String strEnd = YMTDateUtil.getFormatTime(promotionEndTime, "yyyy-MM-dd");
			int promotionType = -4;
			int promotionState = -1;
			
			JSONObject jsonObject = new SellerPromotionListCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionName", promotionName,
					"PromotionBeginTime", strBegin,"PromotionEndTime", strEnd,"PromotionType", promotionType,"PromotionState", promotionState));
			
			// 获取报文返回结果
			JSONObject data = jsonObject.getJSONObject("Data");
			JSONArray promotions = data.getJSONArray("Promotions");
			int code =jsonObject.getInt("Code");
			int size = promotions.length();
			int totalCount = data.getInt("TotalCount");
			
			// 断言
			Logger.verifyEquals(200, code, "响应Code是否等于200");
			Logger.verifyEquals(0, totalCount, "买手无促销活动");
			Logger.verifyEquals(0, size, "活动不存在，结果为空");
			
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionList_006(){
		Logger.start(true, "获取买手促销活动--不存在的活动状态");
		try{

			int sellerId = 20354944;
			String promotionName="满2件";
			Date promotionBeginTime= YMTDateUtil.parseYMDDate("2016-11-15");
			Date promotionEndTime= YMTDateUtil.parseYMDDate("2016-12-14");
			String strBegin = YMTDateUtil.getFormatTime(promotionBeginTime, "yyyy-MM-dd");
			String strEnd = YMTDateUtil.getFormatTime(promotionEndTime, "yyyy-MM-dd");
			int promotionType = -1;
			int promotionState = -6;
			
			JSONObject jsonObject = new SellerPromotionListCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionName", promotionName,
					"PromotionBeginTime", strBegin,"PromotionEndTime", strEnd,"PromotionType", promotionType,"PromotionState", promotionState));
			
			// 获取报文返回结果
			JSONObject data = jsonObject.getJSONObject("Data");
			JSONArray promotions = data.getJSONArray("Promotions");
			int code =jsonObject.getInt("Code");
			int size = promotions.length();
			int totalCount = data.getInt("TotalCount");
			
			// 断言
			Logger.verifyEquals(200, code, "响应Code是否等于200");
			Logger.verifyEquals(0, totalCount, "买手无促销活动");
			Logger.verifyEquals(0, size, "活动不存在，结果为空");
			
			
		}catch (Exception e) {
			Logger.fail(e);
			e.printStackTrace();
		}
		
	}
	
}
